#if UNITY_EDITOR
using UnityEngine;
using UnityEditor;
using System.Diagnostics;
using System.IO;
using System.Collections.Generic;

public class _SVNTools
{

    [MenuItem("Assets/SVN/Log")]
    static void SvnLog()
    {
#if UNITY_STANDALONE_WIN || UNITY_EDITOR
        Process.Start("TortoiseProc.exe", "/command:log /path:" + GetSelectedObjectPath() + " /closeonend:0");
#endif
    }
    [MenuItem("Assets/SVN/保存工程并提交")]
    static void SaveProjectAndSvnCommit()
    {
        ReImportMaterials();
        AssetDatabase.SaveAssets();
        SvnCommit();
    }
    [MenuItem("Assets/SVN/Commit")]
    static void SvnCommit()
    {
        string path = GetSelectedObjectPath();
        //https://docs.microsoft.com/zh-cn/troubleshoot/windows-client/shell-experience/command-line-string-limitation
        //命令行的总长度不能超过 8191 个字符
        if (path.Length > 8000)
        {
            path = path.Replace("*", "\n");
            string pathFilePath = Path.Combine(System.Environment.CurrentDirectory, "svnComitPathFile.txt");
            File.WriteAllText(pathFilePath, path, new System.Text.UnicodeEncoding(false, false));
            string cmd = $"/command:commit /pathfile:{pathFilePath} /deletepathfile";
            Process.Start("TortoiseProc.exe", cmd);
        }
        else
        {
            //防止路径里有空格
            path = "\"" + path + "\"";
            Process.Start("TortoiseProc.exe", "/command:commit /path:" + path + " /closeonend:0");
        }
    }
    private static string GetSelectedObjectPath()
    {
        string path = string.Empty;

        for (int i = 0; i < Selection.assetGUIDs.Length; i++)
        {
            string URL = AssetDatabase.GUIDToAssetPath(Selection.assetGUIDs[i]);
            string[] paths = new string[1];
            paths[0] = URL;
            string[] pathName = AssetDatabase.GetDependencies(paths);
            List<string> pathNameList = new List<string>();
            pathNameList.Add(URL);
            pathNameList.AddRange(pathName);
            for (int j = 0; j < pathNameList.Count; j++)
            {
                path += AssetsPathToFilePath(pathNameList[j]);

                //路径分隔符  
                path += "*";
                //meta文件  
                path += AssetsPathToFilePath(pathNameList[j]) + ".meta";
                //路径分隔符  
                path += "*";
            }
            //path += AssetsPathToFilePath(AssetDatabase.GetAssetPath(Selection.objects[i]));

            ////路径分隔符  
            //path += "*";
            ////meta文件  
            //path += AssetsPathToFilePath(AssetDatabase.GetAssetPath(Selection.objects[i])) + ".meta";
            ////路径分隔符  
            //path += "*";
        }
        return path;
    }
    /// <summary>  
    /// 将Assets路径转换为File路径  
    /// </summary>  
    /// <param name="path">Assets/Editor/...</param>  
    /// <returns></returns>  
    public static string AssetsPathToFilePath(string path)
    {
        string m_path = Application.dataPath;
        m_path = m_path.Substring(0, m_path.Length - 6);
        m_path += path;

        return m_path;
    }
    private static void ReImportMaterials()
    {
        for (int i = 0; i < Selection.objects.Length; i++)
        {
            string path = AssetDatabase.GetAssetPath(Selection.objects[i]);
            //重新导入资源，防止文件名和内部文件名不一致导致Git显示材质修改的问题
            ReImportAssetByExtension(path, ".mat");
        }
    }

    public static void ReImportAssetByExtension(string path, string extension)
    {
        if (Path.GetExtension(path) == extension)
            AssetDatabase.ImportAsset(path, ImportAssetOptions.DontDownloadFromCacheServer);
    }

    [MenuItem("Assets/SVN/Update")]
    static void SvnUpdate()
    {
        Process.Start("TortoiseProc.exe", "/command:update /path:" + GetSelectedObjectPath() + " /closeonend:0");
    }

    [MenuItem("SVN/Update _#&_u")]
    public static void UpdateSVN()
    {
#if UNITY_STANDALONE_WIN||UNITY_EDITOR
        Process.Start("TortoiseProc.exe", "/command:update /path:" + Path.GetDirectoryName(Application.dataPath) + " /closeonend:0");
#endif
    }
    [MenuItem("SVN/Update代码")]
    public static void UpdateSVNCode()
    {
#if UNITY_STANDALONE_WIN||UNITY_EDITOR
        Process.Start("TortoiseProc.exe", "/command:update /path:" + Application.dataPath + "/Scripts /closeonend:0");
#endif
    }
    [MenuItem("SVN/Commit #&m")]
    public static void CommitSVN()
    {
#if UNITY_STANDALONE_WIN||UNITY_EDITOR
        Process.Start("TortoiseProc.exe", "/command:commit /path:" + Path.GetDirectoryName(Application.dataPath) + " /closeonend:0");
#endif
    }
    [MenuItem("SVN/Commit代码")]
    public static void CommitSVNCode()
    {
#if UNITY_STANDALONE_WIN||UNITY_EDITOR
        Process.Start("TortoiseProc.exe", "/command:commit /path:" + Application.dataPath + "/Scripts /closeonend:0");
#endif
    }


}
#endif